package com.example.framework;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class AccountHelper extends WebDriverHelper {

    public AccountHelper(HelperManager manager) {
        super(manager);
    }

    public void signup(User user) {
        openUrl("/");
        click(By.cssSelector("span.bracket-link a[href*=signup_page]"));
        fillSignupForm(user);
        confirmAccountFromMail(user);
        changePassword(user);
    }

    private void changePassword(User user) {
        inputTextToEditBox(By.name("password"), user.password);
        inputTextToEditBox(By.name("password_confirm"), user.password);
        click(By.cssSelector("input.button"));
    }

    private void confirmAccountFromMail(User user) {
        pause(3000);
        Msg msg = manager.getMailHelper().getNewMail(user.login, user.password);
        String confirmationLink = msg.getConfirmationLink();
        openAbsoluteUrl(confirmationLink);
    }

    private void fillSignupForm(User user) {
        inputTextToEditBox(By.name("username"), user.login);
        inputTextToEditBox(By.name("email"), user.email);
        click(By.cssSelector("input.button"));
    }

    public String getLoggedUserLogin() {
        WebElement loginName = driver.findElement(By.cssSelector("td.login-info-left span"));
        return loginName.getText();
    }

    public void login(User user) {
        openUrl("/");
        inputTextToEditBox(By.name("username"), user.login);
        inputTextToEditBox(By.name("password"), user.password);
        click(By.cssSelector("input.button"));
    }
}
